import { EnumRoutePath, EnumRouteTitle } from '@/enum'
import { BlankLayout } from '@/layouts'
import { RouteRecordRaw } from 'vue-router'
import { RouteNameMap } from '../helpers'
import { getLoginModuleRegExp } from '@/utils'
import { LoginModuleType } from '@/interface'

const loginModuleRegExp = getLoginModuleRegExp()

const constantRoutes: RouteRecordRaw[] = [
  {
    name: RouteNameMap.get('system'),
    path: EnumRoutePath.system,
    // 指定了 component，只可能是 RouteRecordSingleView/RouteRecordSingleViewWithChildren
    component: BlankLayout,
    // 又指定了 redirect，必须指定 children，因为 指定 redirect 对应的只能是 RouteRecordSingleViewWithChildren
    // RouteRecordSingleViewWithChildren 中 children 是必须的
    redirect: { name: RouteNameMap.get('not-found') },
    meta: {
      title: EnumRouteTitle.system,
    },
    children: [
      // 登录
      {
        name: RouteNameMap.get('login'),
        // login 使用的是以 / 打头的绝对路径，因此不带 /system 前缀
        path: `${EnumRoutePath.login}/:module(${loginModuleRegExp})?`,
        component: () => import('@/views/system/login/Login.vue'),
        // props 的返回将作为组件的 props
        props: (route) => {
          const moduleType: LoginModuleType =
            (route.params.module as LoginModuleType) || 'pwd-login'
          return {
            module: moduleType,
          }
        },
        meta: {
          fullPage: true,
          title: EnumRouteTitle.login,
        },
      },
      // 403
      {
        name: RouteNameMap.get('no-permission'),
        path: EnumRoutePath['no-permission'],
        component: () => import('@/views/system/exception/Page403.vue'),
        meta: {
          fullPage: true,
          title: EnumRouteTitle['no-permission'],
        },
      },
      // 404
      {
        name: RouteNameMap.get('not-found'),
        path: EnumRoutePath['not-found'],
        component: () => import('@/views/system/exception/Page404.vue'),
        meta: {
          fullPage: true,
          title: EnumRouteTitle['not-found'],
        },
      },
      // 500
      {
        name: RouteNameMap.get('service-error'),
        path: EnumRoutePath['service-error'],
        component: () => import('@/views/system/exception/Page500.vue'),
        meta: {
          fullPage: true,
          title: EnumRouteTitle['service-error'],
        },
      },
    ],
  },
  // 匹配无效的路径重定向到404
  {
    path: '/:pathMatch(.*)*',
    redirect: { name: 'not-found' },
  },
]

export default constantRoutes
